import 'package:flutter/material.dart';
import 'package:flutterdemo/utils/img_util.dart';

class TabBarWidget extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return new TabBarState();
  }
}

class TabBarState extends State<TabBarWidget>
    with SingleTickerProviderStateMixin {
  int selectIndex = 0;

  List<Widget> pages = [];

  @override
  void initState() {
    super.initState();
    pages.add(new HomePage());
    pages.add(new MinePage());
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      bottomNavigationBar: BottomNavigationBar(
        items: <BottomNavigationBarItem>[
          BottomNavigationBarItem(
            icon: Icon(Icons.home, color: Colors.grey),
            activeIcon: Icon(Icons.home, color: Colors.blue),
            title: Text("首页"),
          ),
          BottomNavigationBarItem(
              icon: Icon(Icons.person, color: Colors.grey),
              activeIcon: Icon(Icons.person, color: Colors.blue),
              title: Text("我的")),
        ],
        currentIndex: selectIndex,
        fixedColor: Colors.redAccent,
        unselectedFontSize: 13,
        selectedFontSize: 16,
        unselectedItemColor: Colors.grey,
        onTap: (index) {
          setState(() {
            selectIndex = index;
          });
        },
      ),
      body: pages[selectIndex],
    );
  }
}

class MyDrawer extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Drawer(
      semanticLabel: "label",
      elevation: 10,
      child: MediaQuery.removePadding(
          context: context,
          removeTop: true,
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: <Widget>[
              Padding(
                padding: EdgeInsets.only(top: 50, left: 100),
                child: CircleAvatar(
                  radius: 40,
                  backgroundImage: AssetImage(
                    ImageUtil.getImgPath("img_screen"),
                  ),
                ),
              ),
              Padding(
                padding: EdgeInsets.only(top: 30, left: 20),
                child: Row(
                  children: <Widget>[
                    Padding(
                      padding: EdgeInsets.symmetric(horizontal: 10),
                      child: ClipOval(
                        child: Image.asset(ImageUtil.getImgPath("icon_floor"),
                            width: 30, height: 30),
                      ),
                    ),
                    IconButton(
                      icon: Icon(
                        Icons.favorite_border,
                        color: Colors.redAccent,
                        size: 30,
                      ),
                      onPressed: null,
                    ),
                  ],
                ),
              ),
              Expanded(
                  child: ListView(
                children: <Widget>[
                  ListTile(
                    leading: Icon(
                      Icons.attach_money,
                      color: Colors.amberAccent,
                    ),
                    title: Text("钱包"),
                  ),
                  ListTile(
                    leading: Icon(
                      Icons.shopping_cart,
                      color: Colors.redAccent,
                    ),
                    title: Text("购物车"),
                  ),
                ],
              )),
            ],
          )),
    );
  }
}

class HomePage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return new HomePageState();
  }
}

class HomePageState extends State with SingleTickerProviderStateMixin {
  int selectIndex = 1;
  TabController _tabController;

  @override
  void initState() {
    super.initState();
    _tabController = new TabController(length: 3, vsync: this);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("首页Title"),
        leading: Builder(builder: (context) {
          return IconButton(
            icon: Icon(Icons.menu),
            onPressed: () {
              Scaffold.of(context).openDrawer();
            },
          );
        }),
        bottom: TabBar(
          indicatorColor: Colors.red,
          labelColor: Colors.greenAccent,
          isScrollable: false,
          controller: _tabController,
          tabs: [
            Tab(
              text: "推荐",
              icon: Icon(Icons.person, size: 30, color: Colors.redAccent),
            ),
            Tab(
              text: "视频",
              icon: Icon(Icons.video_call),
            ),
            Tab(
              text: "图片",
              icon: Icon(Icons.image),
            ),
          ],
        ),
      ),
      drawer: new MyDrawer(),
      //TabBarView配合TabBar实现滑动同步
      body: TabBarView(
        controller: _tabController,
        children: <Widget>[
          Container(
            alignment: Alignment.center,
            child: Text("推荐content"),
          ),
          Container(
            alignment: Alignment.center,
            child: Text("视频content"),
          ),
          Container(
            alignment: Alignment.center,
            child: Text("图片content"),
          ),
        ],
      ),
    );
  }
}

class MinePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      child: Text("我的"),
    );
  }
}
